package com.waitingsnow.core.util.display
{
import com.waitingsnow.core.debug.Logger;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.geom.Matrix;
import flash.geom.Rectangle;

/**
 * 
 * @author waitingsnow
 * $Id: BmpDataUtil.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class BmpDataUtil
{
	static public function getBmpDataArr(disObj:DisplayObject, 
										 bdWidth:Number = 0, 
										 bdHeight:Number = 0):Array
	{
		var bmpDataArr:Array = [];
		if (disObj is MovieClip)
		{
			bmpDataArr = startDrawMc(MovieClip(disObj), bdWidth, bdHeight);
		}
		else if (disObj is Bitmap)
		{
			bmpDataArr = [Bitmap(disObj).bitmapData];
		}
		else if (disObj is DisplayObject)
		{
			bmpDataArr = [startDrawSp(disObj, bdWidth, bdHeight)];
		}
		return bmpDataArr;
	}
	
	static public function startDrawMc(disObj:MovieClip, 
									   bdWidth:Number = 0, 
									   bdHeight:Number = 0):Array
	{
		var result:Array = [];
		var totalFrames:int = disObj.totalFrames;
		for (var i:int = 1; i <= totalFrames; i++)
		{
			disObj.gotoAndStop(i);
			result.push(startDrawSp(disObj, bdWidth, bdHeight));
		}
		return result;
	}
	
	static public function startDrawSp(disObj:DisplayObject, 
									   bdWidth:Number = 0, 
									   bdHeight:Number = 0):BitmapData
	{
		bdWidth = bdWidth || disObj.width;
		bdHeight = bdHeight || disObj.height;
		var rect:Rectangle = disObj.getBounds(disObj);
		var mtr:Matrix = new Matrix();
		mtr.tx = -rect.x;
		mtr.ty = -rect.y;
		mtr.scale(bdWidth/disObj.width, bdHeight/disObj.height);
		var bd:BitmapData = new BitmapData(bdWidth, bdHeight, true, 0x00);
		bd.draw(disObj, mtr);
		return bd;
	}
}
}